package com.ruge.test.Redis.jedis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 爱丽丝、如歌  创建于 2018/7/6 19:39
 * 说明:  TODO
 */
@Component
public class JedisUtil {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 获取连接
     */
    private Jedis getResource() {
        return jedisPool.getResource();
    }

    public byte[] set(byte[] key, byte[] serialize) {
        Jedis resource = getResource();
        try {
            resource.set(key, serialize);
        } finally {
            resource.close();
        }
        return serialize;
    }

    public void expire(byte[] key, int i) {
        Jedis resource = getResource();
        try {
            resource.expire(key, i);
        } finally {
            resource.close();
        }
    }

    public byte[] getKey(byte[] key) {
        Jedis resource = getResource();
        try {
            byte[] bytes = resource.get(key);
            return bytes;

        } finally {
            resource.close();
        }

    }

    public void delete(byte[] key) {
        Jedis resource = getResource();
        try {
            resource.del(key);
        } finally {
            resource.close();
        }
    }


    public Set<byte[]> keys(String shiro_session_prefix) {
        Jedis resource = getResource();
        try {
            Set<byte[]> keys = resource.keys((shiro_session_prefix + "*").getBytes());
            return keys;
        } finally {
            resource.close();
        }
    }
}
